package com.zhijian.wharf.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.dao.ThirdPartyDockingMapper;
import com.zhijian.wharf.entity.po.OfficeGroup;
import com.zhijian.wharf.entity.po.ThirdPartyDocking;
import com.zhijian.wharf.entity.vo.request.ThirdPartyDockingSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.request.ThirdPartyDockingSearchVo;
import com.zhijian.wharf.entity.vo.request.UserSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.response.RoleVo;
import com.zhijian.wharf.entity.vo.response.ThirdPartyDockingVo;
import com.zhijian.wharf.enums.OfficeGroupEnum;
import com.zhijian.wharf.enums.RoleEnum;
import com.zhijian.wharf.enums.ThirdPartyDockingStatusEnum;
import com.zhijian.wharf.enums.UserStatusEnum;
import com.zhijian.wharf.service.OfficeGroupService;
import com.zhijian.wharf.service.RoleService;
import com.zhijian.wharf.service.ThirdPartyDockingService;
import com.zhijian.wharf.service.UserService;
import com.zhijian.wharf.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 第三方对接业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-07-07 13:34:55
 */
@Slf4j
@Service
public class ThirdPartyDockingServiceImpl implements ThirdPartyDockingService {

    @Autowired
    private ThirdPartyDockingMapper mapper;

    @Autowired
    private UserService userService;

    @Autowired
    private OfficeGroupService officeGroupService;

    @Autowired
    private RoleService roleService;

    @Override
    public ThirdPartyDocking selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ThirdPartyDockingVo selectVoById(Long id) {
        ThirdPartyDockingVo thirdPartyDockingVo = mapper.selectVoById(id);
        if (Objects.isNull(thirdPartyDockingVo)) {
            return null;
        }
        String modules = thirdPartyDockingVo.getModules();
        if (StringUtils.isNotBlank(modules)) {
            thirdPartyDockingVo.setModuleList(Arrays.stream(modules.split(",")).map(Integer::valueOf).collect(Collectors.toList()));
        }
        return thirdPartyDockingVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(ThirdPartyDocking thirdPartyDocking) {
        Date now = new Date();
        thirdPartyDocking.setCreateTime(now);
        thirdPartyDocking.setUpdateTime(now);
        return mapper.insert(thirdPartyDocking) > 0 ? thirdPartyDocking.getId() : 0L;
    }

    @Override
    public Long insertSelective(ThirdPartyDocking thirdPartyDocking) {
        Date now = new Date();
        thirdPartyDocking.setCreateTime(now);
        thirdPartyDocking.setUpdateTime(now);
        return mapper.insertSelective(thirdPartyDocking) > 0 ? thirdPartyDocking.getId() : 0L;
    }

    @Override
    public boolean update(ThirdPartyDocking thirdPartyDocking) {
        Date now = new Date();
        thirdPartyDocking.setUpdateTime(now);
        return mapper.updateByPrimaryKey(thirdPartyDocking) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(ThirdPartyDocking record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ThirdPartyDocking save(ThirdPartyDockingSaveOrUpdateVo saveOrUpdateVo) {
        UserSaveOrUpdateVo userSaveOrUpdateVo = new UserSaveOrUpdateVo();
        userSaveOrUpdateVo.setName(saveOrUpdateVo.getName());
        userSaveOrUpdateVo.setNickName(saveOrUpdateVo.getName());
        userSaveOrUpdateVo.setPhone(saveOrUpdateVo.getPhone());
        userSaveOrUpdateVo.setStatus(UserStatusEnum.NORMAL.getCode().byteValue());
        //部门
        OfficeGroup officeGroup = officeGroupService.selectByName(OfficeGroupEnum.REGISTER.getDesc());
        AssertUtil.notNull(officeGroup, "部门初始化失败");
        userSaveOrUpdateVo.setGroupId(officeGroup.getId());
        //角色
        RoleVo roleVo = roleService.selectVoByName(RoleEnum.REGISTER.getDesc());
        AssertUtil.notNull(roleVo, "角色初始化失败");
        userSaveOrUpdateVo.setRoleIds(Collections.singletonList(roleVo.getId()));
        Long userId = userService.insert(userSaveOrUpdateVo);
        ThirdPartyDocking thirdPartyDocking = new ThirdPartyDocking();
        BeanUtils.copyProperties(saveOrUpdateVo, thirdPartyDocking);
        thirdPartyDocking.setUserId(userId);
        thirdPartyDocking.setModules(StrUtil.join(",", saveOrUpdateVo.getModuleList()));
        thirdPartyDocking.setAppId(RandomUtil.randomString(10));
        thirdPartyDocking.setAppSecret(RandomUtil.randomString(20));
        this.insert(thirdPartyDocking);
        return thirdPartyDocking;
    }

    @Override
    public boolean register(ThirdPartyDockingSaveOrUpdateVo saveOrUpdateVo) {
        //默认待审核
        saveOrUpdateVo.setStatus(ThirdPartyDockingStatusEnum.WAIT_APPROVE.getCode().byteValue());
        return save(saveOrUpdateVo) != null;
    }

    @Override
    public boolean modify(ThirdPartyDockingSaveOrUpdateVo saveOrUpdateVo) {
        ThirdPartyDocking old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        ThirdPartyDocking thirdPartyDocking = new ThirdPartyDocking();
        BeanUtils.copyProperties(saveOrUpdateVo, thirdPartyDocking);
        return this.update(thirdPartyDocking);
    }

    @Override
    public ThirdPartyDockingVo selectOne(ThirdPartyDockingSearchVo thirdPartyDockingSearchVo) {
        return mapper.selectOne(thirdPartyDockingSearchVo);
    }

    @Override
    public Integer count(ThirdPartyDockingSearchVo thirdPartyDockingSearchVo) {
        return mapper.count(thirdPartyDockingSearchVo);
    }

    @Override
    public List<ThirdPartyDockingVo> selectList(ThirdPartyDockingSearchVo thirdPartyDockingSearchVo) {
        return mapper.selectList(thirdPartyDockingSearchVo);
    }

    @Override
    public Page<ThirdPartyDockingVo> selectPage(ThirdPartyDockingSearchVo thirdPartyDockingSearchVo) {
        AssertUtil.check(Objects.isNull(thirdPartyDockingSearchVo.getPageNum()) || Objects.isNull(thirdPartyDockingSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(thirdPartyDockingSearchVo);
        //分页
        Page<ThirdPartyDockingVo> page = new Page<>(true, count, thirdPartyDockingSearchVo.getPageNum(), thirdPartyDockingSearchVo.getPageSize());
        thirdPartyDockingSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ThirdPartyDockingVo> responseVos = this.selectList(thirdPartyDockingSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<ThirdPartyDocking> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(ThirdPartyDockingSearchVo thirdPartyDockingSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(thirdPartyDockingSearchVo);
    }

    @Override
    public boolean approve(Long id, Byte approveStatus) {
        ThirdPartyDocking company = this.selectById(id);
        AssertUtil.notNull(company, "此数据不存在或者已被删除");
        //修改
        return this.updateByPrimaryKeySelective(ThirdPartyDocking.builder()
                .id(id)
                .status(approveStatus)
                .build());
    }

}